package cn.niit.controller;

import cn.niit.entity.Course;
import cn.niit.entity.CourseTime;
import cn.niit.entity.SysUser;
import cn.niit.entity.query.CourseQuery;
import cn.niit.entity.query.ScoreQuery;
import cn.niit.entity.query.SysUserQuery;
import cn.niit.service.SysUserService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassNameSysUserController
 * @Description TODO
 * @Author田嘉
 * @Date2021/12/110:48
 * @Version 1.0
 **/
@Controller
@RequestMapping("/user")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;
    @GetMapping("/query")
    public String queryByUser(Model model, SysUserQuery sysUserQuery) {
        PageInfo<SysUser> sysUserPageInfo = sysUserService.queryByUser(sysUserQuery);
        model.addAttribute("page", sysUserPageInfo);
        return "/user/user";
    }
    @GetMapping("/select")
    public String select(Model model,SysUserQuery sysUserQuery){
        PageInfo<SysUser> sysUserPageInfo = sysUserService.select(sysUserQuery);
        model.addAttribute("page", sysUserPageInfo);
        return "/user/user";
    }

    @GetMapping("/edit/{id}")
    public String toEdit(@PathVariable("id") Long id, Model model) {
        model.addAttribute("user", sysUserService.queryUserById(id));
        return "/user/editUser";
    }

//    @PostMapping("/edit")
//    public String edit(SysUser sysUser) {
//        SysUserQuery sysUserQuery = new SysUserQuery();
//        Long id = sysUser.getId();
//        sysUserQuery.setUserName(sysUser.getUserName());
//        PageInfo<SysUser> sysUserPageInfo= sysUserService.queryByUserName(sysUserQuery);
//        boolean b;
//        if (id != null) {
//            b = sysUserService.editUser(sysUser);
//        } else {
//            b = sysUserService.addUser(sysUser);
//        }
//        return "redirect:/user/query";
//    }
//
//    @GetMapping("/update")
//    public String update(Model model) {
//        SysUser sysUser=new SysUser();
//        model.addAttribute("user", sysUser);
//        return "/user/editUser";
//    }
@GetMapping("/register")
public String register(){
        return "register";
}
@PostMapping("/register")
public String toRegister(SysUser sysUser){
        sysUserService.addUser(sysUser);
        return "redirect:/";
}
    @GetMapping("/delete/{id}")
    public String deleteUserById(@PathVariable("id") Long id) {
        boolean b = sysUserService.deleteUserById(id);
        return "redirect:/user/query";
}}
